package com.bupt.liutong.admin.sch.bo;

import java.io.File;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.upload.FormFile;
import org.springframework.transaction.annotation.Transactional;

import com.bupt.liutong.admin.core.base.BaseBo;
import com.bupt.liutong.admin.core.base.FileUploader;
import com.bupt.liutong.admin.sch.dao.TeacherDao;
import com.bupt.liutong.admin.sch.dao.TeacherPhotoDao;
import com.bupt.liutong.admin.sch.dto.Teacher;
import com.bupt.liutong.admin.sch.form.TeacherForm;
import com.bupt.liutong.util.BeanUtils;
import com.bupt.liutong.util.FileUtils;
import com.bupt.liutong.util.StringUtils;

public class TeacherBo extends BaseBo {

	private TeacherDao teacherDao;
	private TeacherPhotoDao teacherPhotoDao;
	private FileUploader fileUploader;

	public List<TeacherForm> getTeachersByPage(TeacherForm teacherForm,
			HttpServletRequest request) {
		Teacher teacher = new Teacher();
		sm.pagingWithNoCriteria(teacherForm, teacher, "sch_teacher.getTeachersCount");
		List<Teacher> teachers = teacherDao.getTeachersByPage(teacher);
		return BeanUtils.dtoToForm(teachers, TeacherForm.class);
	}

	public List<TeacherForm> getTeachersByPageAtFront(TeacherForm teacherForm,
			HttpServletRequest request) {
		Teacher teacher = new Teacher();
		sm.pagingWithNoCriteria(teacherForm, teacher, "sch_teacher.getTeachersCount");
		if (teacher.getCurPage() != 1
				&& teacher.getStartRow() + teacher.getPageSize() - 1 > teacher.getTotalRow()) {
			teacher.setStartRow(teacher.getTotalRow() - teacher.getPageSize());
		}
		List<Teacher> teachers = teacherDao.getTeachersByPage(teacher);
		return BeanUtils.dtoToForm(teachers, TeacherForm.class);
	}

	public TeacherForm getTeacherById(TeacherForm teacherForm) {
		TeacherForm resultForm = new TeacherForm();
		Teacher teacher = teacherDao.getTeacherById(teacherForm.getId());
		BeanUtils.copyProperties(resultForm, teacher);
		return resultForm;
	}

	@Transactional
	public void insertTeacher(TeacherForm teacherForm, HttpServletRequest request) {

		String fileName = teacherForm.getMainPicFile().getFileName();
		if (teacherForm.getMainPicFile() != null) {
			// 时间戳
			long timeStamp = new Date().getTime();
			// 文件名中文转拼音
			if (StringUtils.containsChinese(fileName))
				fileName = StringUtils.toPinyin(fileName);
			fileName = timeStamp + "_" + fileName;
			teacherForm.setMainPic(fileUploader.getUrl(request) + "/image/teacher/"
					+ teacherForm.getId() + "/" + fileName);
		}
		
		Teacher teacher = new Teacher();
		BeanUtils.copyProperties(teacher, teacherForm);
		Integer teacherId = teacherDao.insertTeacher(teacher);
		teacherForm.setId(teacherId);
		
		if (teacherForm.getMainPicFile() != null) {
			fileUploader.uploadImages(teacherForm, teacherPhotoDao, request);
			// 上传教师主图
			try {
				FormFile mainPic = teacherForm.getMainPicFile();
				FileUtils.forceCopyFile(mainPic.getInputStream(), fileUploader.getPath(request)
						+ File.separator
						+ "image"
						+ File.separator
						+ "teacher"
						+ File.separator
						+ teacher.getId()
						+ File.separator
						+ fileName);
			} catch (Exception e) {
				e.printStackTrace();
				throw new RuntimeException();
			}
		}
	}

	@Transactional
	public void updateTeacher(TeacherForm teacherForm, HttpServletRequest request) {

		String fileName = teacherForm.getMainPicFile().getFileName();
		if (!StringUtils.isEmpty(fileName)) {
			// 时间戳
			long timeStamp = new Date().getTime();
			// 文件名中文转拼音
			if (StringUtils.containsChinese(fileName))
				fileName = StringUtils.toPinyin(fileName);
			fileName = timeStamp + "_" + fileName;
			teacherForm.setMainPic(fileUploader.getUrl(request) + "/image/teacher/"
					+ teacherForm.getId() + "/" + fileName);
		}
		
		//update basic info
		Teacher teacher = new Teacher();
		BeanUtils.copyProperties(teacher, teacherForm);
		teacherDao.updateTeacher(teacher);

		if (!StringUtils.isEmpty(fileName)) {
			// update old photo
			fileUploader.updateImages(teacherForm, teacherPhotoDao, request);
			// upload new photo
			fileUploader.uploadImages(teacherForm, teacherPhotoDao, request);
			// 上传教师主图
			try {
				FormFile mainPic = teacherForm.getMainPicFile();
				FileUtils.forceCopyFile(mainPic.getInputStream(), fileUploader.getPath(request)
						+ File.separator
						+ "image"
						+ File.separator
						+ "teacher"
						+ File.separator
						+ teacher.getId()
						+ File.separator
						+ fileName);
			} catch (Exception e) {
				e.printStackTrace();
				throw new RuntimeException();
			}
		}
	}

	@Transactional
	public void deleteTeachers(String[] ids) {
		if (ids != null) {
			for (String id : ids) {
				teacherDao.deleteTeacher(Integer.valueOf(id));
			}
		}
	}

	public void setTeacherDao(TeacherDao teacherDao) {
		this.teacherDao = teacherDao;
	}

	public void setTeacherPhotoDao(TeacherPhotoDao teacherPhotoDao) {
		this.teacherPhotoDao = teacherPhotoDao;
	}

	public void setFileUploader(FileUploader fileUploader) {
		this.fileUploader = fileUploader;
	}
}
